package com.slz.crm.server.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.slz.crm.common.annotation.RequirePermission;
import com.slz.crm.common.enumeration.PermissionOperates;
import com.slz.crm.common.result.Result;
import com.slz.crm.pojo.dto.ContractANDOrderDTO;
import com.slz.crm.pojo.dto.ContractDTO;
import com.slz.crm.pojo.dto.OrderDTO;
import com.slz.crm.pojo.vo.BusinessActivityVO;
import com.slz.crm.pojo.vo.ContractANDOrderVO;
import com.slz.crm.pojo.vo.ContractVO;
import com.slz.crm.server.service.ContractOrderItemService;
import com.slz.crm.server.service.ContractService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;

/**
 * 合同
 */
@RestController
@RequestMapping("/contract")
@Slf4j
public class ContractController {

    @Autowired
    private ContractService contractService;

    @Autowired
    private ContractOrderItemService contractOrderItemService;

    /**
     * 新增合同
     * @param dto 合同AND订单DTO
     * @return 合同VO
     */
    @PostMapping
    @RequirePermission(
            permClass = PermissionOperates.SalesOperate.class,
            permName = "CREATE_CONTRACT"
    )
    public Result<ContractVO> create(@RequestBody ContractANDOrderDTO dto) {

        ContractVO contractVO = contractService.create(dto.getContract());

        List<OrderDTO> orders = dto.getOrders();
        orders.forEach(order -> {
            order.setContractId(contractVO.getId());
        });
        Integer count = contractOrderItemService.createBatch(orders);

        return Result.success(contractVO);

    }

    /**
     * 根据ID查询合同详情（包含订单信息）
     * @param id 合同ID
     * @return 包含订单信息的合同详情
     */
    @GetMapping("/{id}")
    @RequirePermission(
            permClass = PermissionOperates.SalesOperate.class,
            permName = "VIEW_CONTRACT_DETAIL_BY_ID"
    )
    public Result<ContractANDOrderVO> getDetailById(@PathVariable Long id) {
        ContractANDOrderVO contractDetail = contractService.getDetailById(id);
        return Result.success(contractDetail);
    }

    /**
     * 自定义查询合同列表（带条件）
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @param dto 查询条件
     * @return 分页结果
     */
    @PostMapping("/query")
    @RequirePermission(
            permClass = PermissionOperates.SalesOperate.class,
            permName = "CUSTOM_QUERY_CONTRACT"
    )
    public Result<Page<ContractVO>> contractQuery(@RequestParam Integer pageNum, @RequestParam Integer pageSize, @RequestBody ContractDTO dto) {
        Page<ContractVO> page = contractService.contractQuery(pageNum, pageSize, dto);
        return Result.success(page);
    }

    /**
     * 更新合同信息
     * @param dto 合同数据
     * @return 更新结果
     */
    @PutMapping
    @RequirePermission(
            permClass = PermissionOperates.SalesOperate.class,
            permName = "UPDATE_CONTRACT"
    )
    public Result<Boolean> update(@RequestBody ContractDTO dto) {
        boolean result = contractService.update(dto);
        return Result.success(result);
    }

    /**
     * 批量删除合同
     * @param ids 合同ID列表
     * @return 成功删除的数量
     */
    @DeleteMapping
    @RequirePermission(
            permClass = PermissionOperates.SalesOperate.class,
            permName = "BATCH_DELETE_CONTRACT"
    )
    public Result<Integer> delete(@RequestBody List<Long> ids) {
        try {
            int count = contractService.batchDelete(ids);
            return Result.success(count);
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取所有合同
     * @param pageNum 页码
     * @param pageSize 每页数量
     * @return 合同列表
     */
    @GetMapping
    @RequirePermission(
            permClass = PermissionOperates.SalesOperate.class,
            permName = "VIEW_CONTRACT"
    )
    public Result<Page<ContractVO>> getAllContract(@RequestParam Integer pageNum, @RequestParam Integer pageSize){
        if (pageNum == null || pageSize  == null || pageNum <= 0 || pageSize <= 0) {
            pageNum = 1;
            pageSize = 10;
        }
        return Result.success(contractService.getAllContract(pageNum, pageSize));
    }

}
